package com.vvv.designpattern.synchronizedpattern;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;

/**
 * @author vvv
 * @date 2023-10-17 10 52
 * discription
 */
public class AlternatingParkUnPark {
    static Thread t1;
    static Thread t2;
    static Thread t3;

    public static void main(String[] args) {
        ParkUnPark parkUnPark = new ParkUnPark(5);
        t1 = new Thread(() -> {
            parkUnPark.print("a", t2);
        });
        t2 = new Thread(() -> {
            parkUnPark.print("b", t3);
        });
        t3 = new Thread(() -> {
            parkUnPark.print("c", t1);
        });
        t1.start();
        t2.start();
        t3.start();
        LockSupport.unpark(t1);
    }
}

class ParkUnPark {
    // 循环次数
    private Integer loopNumber;

    public void print(String output, Thread next) {
        for (int i = 0; i < loopNumber; i++) {
            LockSupport.park();
            System.out.print(output);
            LockSupport.unpark(next);
        }
    }

    public ParkUnPark(Integer loopNumber) {
        this.loopNumber = loopNumber;
    }
}
